package com.tckdq.tcc.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Utf8;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import com.tckdq.tcc.constants.MQConstants;
import com.tckdq.tcc.dto.StationMessage2MQDto;
import com.tckdq.tcc.service.IMessageStationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
@RocketMQMessageListener(
        consumerGroup = "service-common-station",
        topic = MQConstants.TOPIC_STATION_FILE,
        selectorExpression = MQConstants.TAGS_STATION_FILE
)
public class StationConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMessageStationService messageStationService;



    @Override
    public void onMessage(MessageExt messageExt) {
        if (messageExt.getBody()==null ||messageExt.getBody().length==0)return;

        //获取消息
        String message = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        StationMessage2MQDto message2MQDto = JSON.parseObject(message, StationMessage2MQDto.class);
        //保存站内信
        messageStationService.saveStationMessage(message2MQDto);
        log.info("保存站内信 {}",message2MQDto);
    }
}
